//
//  Data+Tool.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/6/10.
//

import Foundation

extension Data{
    
    func toString() -> String?{
        
        return self.toAny() as? String
    }
    func toDictionary() -> Dictionary<String, Any>?{
        
        return self.toAny() as?  Dictionary<String,Any>
    }
    func toArray() -> Array<Any>?{
        
        return self.toAny() as? Array<Any>
    }
    
    func toAny() -> Any?{
        
        do{
            return try JSONSerialization.jsonObject(with: self,options: [.mutableContainers,.fragmentsAllowed])
        }
        catch{
            debugPrint("Data转Dictionary失败",error)
            return nil
        }
    }
}
